//
//  HStypeDetailVC.m
//  hsdyOrderSystem
//
//  Created by XunFengKJ on 2017/11/28.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HStypeDetailVC.h"
#import "HSTypeDetailModel.h"
#import "HSAddOrderViewController.h"

@interface HStypeDetailVC ()
@property (strong, nonatomic) NSMutableArray *chooseTypeDataArr;//数据
@property (strong, nonatomic)NSMutableArray *readySelectTypeM;//已选的type
@property (strong, nonatomic)NSMutableArray *readySelectTypeNameM;//已选的typeName
@end

@implementation HStypeDetailVC

-(void)dealloc{
    lNOTIF_REMV();
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.model.name;
    [self loadTypeDataSource];
    lNOTIF_ADD(@"htchangeShopItemSuccessNoti", htchangeShopItemSuccessNoti);//修改菜品成功
    lNOTIF_ADD(@"shopSallAllSuccessNoti", htchangeShopItemSuccessNoti);//菜品卖完了
    lNOTIF_ADD(@"shopcanSallSuccessNoti", htchangeShopItemSuccessNoti);//菜品可以卖了
    [self startPullDownRefreshing];
}

-(void)htchangeShopItemSuccessNoti{
    [self loadDataSource];
}

-(void)initViews{
    [self initTableView];
    [self headerRereshing];
}

- (void)initTableView{
    [self initTableViewWithStyle:UITableViewStylePlain];
    self.tableView.backgroundColor = XFUIColorFromRGB(0xf0f2f5);
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    //设置cell的估计高度
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

#pragma mark - Data Source
- (void)loadDefaultDataSource{
    [super loadDefaultDataSource];
    self.blankHintImage = [UIImage imageNamed:@""];
    self.blankHintString = @"暂无菜品";
}

-(void)loadDataSource{
    
    WEAKSELF
    if (self.requestCurrentPage!=1) {
        [self.HUD showAnimated:YES];
    }
    [XFPostRequest postfindFoodsByItemShopId:[BookShareUserInforDefault readUserUid] foodItems:lIntToString(self.model.type_id) success:^(id responseObject) {
//        LGFLog(@"===%@",responseObject);
        [weakSelf hideLoading];
        if (responseObject == nil)  return ;
        if ([responseObject[@"status"] intValue] == 200)
        {

            [weakSelf addToDataSource:[HSTypeDetailModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]]];
        }
        else {
            if (![[HSTypeDetailModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]] isKindOfClass:[NSArray class]]) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf reloadDataWithBlank];
            }
            [weakSelf endPullDownRefreshing];
            [weakSelf setIsPageEnd:YES];
        }
        [weakSelf.tableView reloadData];
    } failure:^(NSError *error) {
        [weakSelf hideLoading];
        [MBProgressHUD showError:LNETWORKERROR];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    HSTypeDetailModel * model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.readySelectTypeNameM removeAllObjects];
    [self.readySelectTypeM removeAllObjects];
    [self findTypeByfoodsIdIndexPath:indexPath];
}

-(NSMutableArray *)chooseTypeDataArr{
    if (!_chooseTypeDataArr) {
        _chooseTypeDataArr = @[].mutableCopy;
    }
    return _chooseTypeDataArr;
}

#pragma mark - 获取分类数据
-(void)loadTypeDataSource{

    [self showLoadingWithClear:YES];
    WEAKSELF
    [XFPostRequest postShopFootTypeshopId:[BookShareUserInforDefault readUserUid] success:^(id responseObject) {
        if ([responseObject[@"status"] intValue] == 200){
            [weakSelf.chooseTypeDataArr removeAllObjects];
            [weakSelf hideLoading];
            [weakSelf.chooseTypeDataArr addObjectsFromArray: [HSFoodtypeModel mj_objectArrayWithKeyValuesArray:[responseObject safeJsonObjectForKey:@"content"]]];
        }else {
            if (![[HSFoodtypeModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]] isKindOfClass:[NSArray class]]) {
                [weakSelf.chooseTypeDataArr removeAllObjects];
            }
        }
    } failure:^(NSError *error) {
        [weakSelf hideLoading];
        [MBProgressHUD showError:LNETWORKERROR];
        LGFLog(@"error = %@",error);
    }];
}

#pragma mark - 获取已选分类
-(void)findTypeByfoodsIdIndexPath:(NSIndexPath *)indexPath{
    [self showLoadingWithClear:YES];
    HSAddOrderViewController *orderVC = [HSAddOrderViewController new];
    HSTypeDetailModel *model= self.dataSource[indexPath.row];
    orderVC.model =model;
    orderVC.isFromTypeDetailVC = YES;
    orderVC.typeModel = self.model;
    WEAKSELF
    [XFPostRequest postfindTypeByfoodsId:lIntToString(model.typeDetail_id) success:^(id responseObject) {
        if ([responseObject[@"status"] intValue] == 200){
            [weakSelf hideLoading];
            LGFLog(@"responseObject = %@",responseObject);
            
            NSArray * arr = [responseObject safeJsonObjectForKey:@"content"];
            for (int i = 0; i<arr.count; i++) {
                NSString *tyid = arr[i];
                NSInteger typeid = [tyid integerValue];
                for (int j=0; j<weakSelf.chooseTypeDataArr.count ; j++) {
                    HSFoodtypeModel *model = weakSelf.chooseTypeDataArr[j];
                    LGFLog(@"type_id %zd = %zd",typeid,model.type_id);
                    if (typeid == model.type_id) {
                        [weakSelf.readySelectTypeNameM addObject:model.name];//得到选中相应的下标名称
                        [weakSelf.readySelectTypeM addObject:lIntToString(model.type_id)];//得到选中相应的下标
                    }
                }
            }
            orderVC.readySelectTypeNameM =weakSelf.readySelectTypeNameM;
            orderVC.readySelectTypeM =weakSelf.readySelectTypeM;
            LGFLog(@"%@===%@",orderVC.readySelectTypeNameM,orderVC.readySelectTypeM);
            [weakSelf.navigationController pushViewController:orderVC animated:YES];
        }
        else {
        }
    } failure:^(NSError *error) {
        [weakSelf hideLoading];
        [MBProgressHUD showError:LNETWORKERROR];
        LGFLog(@"error = %@",error);
    }];
}

#pragma mark - lazy
-(NSMutableArray *)readySelectTypeM{
    if (!_readySelectTypeM) {
        _readySelectTypeM = @[].mutableCopy;
    }
    return _readySelectTypeM;
}

-(NSMutableArray *)readySelectTypeNameM{
    if (!_readySelectTypeNameM) {
        _readySelectTypeNameM = @[].mutableCopy;
    }
    return _readySelectTypeNameM;
}

@end
